package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.query.JobseekerJobQO;
import com.xingkeduo.pojo.table.JobseekerJobTO;
import com.xingkeduo.pojo.utils.Base64Utils;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.RecruitSourceEnum;
import com.xingkeduo.util.pojo.enums.WorkYearEnum;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.RegularUtil;
import org.springframework.stereotype.Component;

/**
 * 求职者工作信息封装类
 *
 * @author 高杰
 * @time 2018年12月25日 下午1:45:17
 * @description
 */
@Component
public class JobseekerJobJTO extends BaseJson {
    public static JobseekerJobTO createTableForNewDetail(String json) throws Throwable {
        JobseekerJobTO table = new JobseekerJobTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数格式");

        table.setId(validId(root, YesNoEnum.YES)).setCode(validCode(root, YesNoEnum.YES))
                .setYears(validYears(root, YesNoEnum.YES));

        return table;
    }

    public static JobseekerJobTO createTableForNew(String json) throws Throwable {
        JobseekerJobTO table = new JobseekerJobTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数格式");

        table.setId(validId(root, YesNoEnum.YES)).setCode(validCode(root, YesNoEnum.YES))
                .setSalary(validSalary(root, YesNoEnum.YES)).setReferrer(validReferrer(root, YesNoEnum.NO))
                .setSource(validSource(root, YesNoEnum.YES)).setContactName(validContactName(root, YesNoEnum.NO))
                .setContactPhone(validContactPhone(root, YesNoEnum.NO)).setCityId(validCityId(root, YesNoEnum.YES))
                .setIsStays(validIsStays(root, YesNoEnum.YES));

        return table;
    }

    public static JobseekerJobTO createTableForModify(String json) throws Throwable {
        JobseekerJobTO table = new JobseekerJobTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数格式");

        table.setYears(validYears(root, YesNoEnum.NO)).setSalary(validSalary(root, YesNoEnum.NO))
                .setReferrer(validReferrer(root, YesNoEnum.NO)).setSource(validSource(root, YesNoEnum.NO))
                .setContactName(validContactName(root, YesNoEnum.NO))
                .setContactPhone(validContactPhone(root, YesNoEnum.NO)).setCityId(validCityId(root, YesNoEnum.NO));
        return table;
    }

    public static JobseekerJobQO createQueryForPrimary(String json) throws Throwable {
        JobseekerJobQO query = new JobseekerJobQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;

        query.setId(validId(root, YesNoEnum.YES));
        return query;
    }

    public static JobseekerJobQO createQueryForList(String json) throws Throwable {
        JobseekerJobQO query = new JobseekerJobQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;

        query.setCityId(validCityId(root, YesNoEnum.NO));
        return query;
    }

    public static ObjectNode toJson(JobseekerJobTO table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();

        if (table != null) {
            if (table.getYears() != null) {
                root.put("years_value", table.getYears().getValue());
                root.put("years_desc", table.getYears().getDesc());
            }
            if (table.getSalary() != null)
                root.put("salary", table.getSalary());
            if (table.getReferrer() != null)
                root.put("referrer", table.getReferrer());
            if (table.getSource() != null) {
                root.put("source_value", table.getSource().getValue());
                root.put("source_desc", table.getSource().getDesc());
            }
            if (table.getWechatName() != null)
                root.put("wechatName", Base64Utils.decoderParam(table.getWechatName()));
            if (table.getContactName() != null)
                root.put("contactname", table.getContactName());
            if (table.getContactPhone() != null)
                root.put("contactphone", table.getContactPhone());
            if (0 != table.getCityId()) {
                root.put("city_id", table.getCityId());
            } else {
                root.put("city_id", 0);
            }
            if (table.getIsStays() != null) {
                root.put("isstays_value", table.getIsStays().getValue());
                root.put("isstays_desc", table.getIsStays().getDesc());
            }
            toBaseJson(root, table);
            if (!CommonUtil.isEmpty(table.getDetails()))
                root.putPOJO("details", JobseekerJobDetailJTO.toJson(table.getDetails()));
        }

        return root;
    }

    public static WorkYearEnum validYears(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "years_value";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!WorkYearEnum.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return WorkYearEnum.getWorkYear(value);
        }
        return null;
    }

    public static String validSalary(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "salary";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 20)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static String validReferrer(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "referrer";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (null == value || ("").equals(value)) {
                return "无";
            }
            if (value.length() > 50)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static RecruitSourceEnum validSource(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "source_value";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!RecruitSourceEnum.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return RecruitSourceEnum.getRecruitSource(value);
        }
        return null;
    }

    public static String validContactName(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "contactname";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 20)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static String validContactPhone(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "contactphone";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 20)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static long validCityId(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "city_id";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (Long.parseLong(value) == 0) {
                return 0;
            }
            if (!RegularUtil.isPositiveInteger(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return Long.parseLong(value);
        }
        return 0;
    }

    public static YesNoEnum validIsStays(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "isstays_value";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!YesNoEnum.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return YesNoEnum.getYesNo(value);
        }
        return null;
    }
}
